﻿namespace In66.Net.Share.Authentication.Handlers
{
    public class BasicTokenGenerator : ITokenGenerator
    {
        private readonly AuthenticationInfo? _userContext;
        private readonly ILogger<BasicTokenGenerator> _logger;

        public BasicTokenGenerator(
            IHttpContextAccessor httpContextAccessor,
            ILogger<BasicTokenGenerator> logger)
        {
            _userContext = httpContextAccessor.HttpContext.RequestServices.GetService<AuthenticationInfo>();
            _logger = logger;
        }

        public static string Scheme => "Basic";

        public string GeneratorName => Scheme;

        public virtual string Create()
        {
            string userName;

            if (_userContext is null)
            {
                userName = string.Empty;
            }
            else
            {
                userName = _userContext.Name;
            }

            _logger.LogDebug($"UserContext:{userName}");

            var token = BasicTokenValidator.PackToBase64(userName);

            return token;
        }
    }
}
